#include "ItemWidget.h"
#include "BitWidget.h"

#include <QLabel>
#include <QLineEdit>
#include <QComboBox>
#include <QGridLayout>
#include <QSizePolicy>
#include <QEvent>
#include <QDialog>

ItemWidget::ItemWidget(const int num , const QString& strLabelNameText,const int value,const int selection):
    strLableNameText(strLabelNameText),
    value(value),
    selection(selection),
    Num(num)
{
    customUI();
}

ItemWidget::~ItemWidget()
{

}
/*
* @brief 创建界面
* @note
*/
void ItemWidget::customUI()
{
    QWidget* widget = new QWidget();
    QGridLayout* mainlayout = new QGridLayout();
    labText = new QLabel();
    labText->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
    labText->setText(QString::number(Num + 1) + ": " + strLableNameText);

    leVal = new QLineEdit();
    leVal->setText(QString::number(value));
    leVal->setWhatsThis("d");
    leVal->installEventFilter(this);

    cbSelection = new QComboBox();
    cbSelection->addItem("十进制");
    cbSelection->addItem("十六进制");
    cbSelection->addItem("二进制");

    mainlayout->addWidget(labText,0,0);
    mainlayout->addWidget(leVal,1,0);
    mainlayout->addWidget(cbSelection,2,0);

    widget->setLayout(mainlayout);
    widget->setStyleSheet("background-color: rgb(211,211,211);");
    setCentralWidget(widget);
    connect(cbSelection,SIGNAL(currentIndexChanged(int)),this,SLOT(indexchangedHandler(int)));
    connect(leVal,SIGNAL(textChanged(const QString &)),this,SLOT(valuechangedHandler(const QString&)));
}
/*
* @brief  下拉栏索引变化修改lineedit中值
* @note
*/
void ItemWidget::indexchangedHandler(int index)
{
    quint64 ulValue = 0U;
    bool ok = false;
    if(leVal->whatsThis().contains("h"))
    {
        ulValue = leVal->text().toULongLong(&ok,16);
    }
    else if(leVal->whatsThis().contains("b"))
    {
        ulValue = leVal->text().toULongLong(&ok,2);
    }
    else
    {
        ulValue = leVal->text().toULongLong(&ok,10);
    }

    QString strLeVal = "";
    switch(index)
    {
    case 0://十进制
        strLeVal = QString::number(ulValue,10);
        leVal->setWhatsThis("d");
        break;
    case 1://十六进制
        strLeVal = QString::number(ulValue,16);
        leVal->setWhatsThis("h");
        break;
    case 2://二进制
        strLeVal = QString::number(ulValue,2);
        leVal->setWhatsThis("b");
        break;
    default:
        strLeVal = QString::number(ulValue);
        leVal->setWhatsThis("d");
        break;
    }
    leVal->setText(strLeVal);
}
/*
* @brief
* @note
*/
void ItemWidget::valuechangedHandler(const QString& text)
{
    quint64 Value = 0U;
    bool ok = false;
    if(leVal->whatsThis().contains("h"))
    {
        Value = leVal->text().toULongLong(&ok,16);
    }
    else if(leVal->whatsThis().contains("b"))
    {
        Value = leVal->text().toULongLong(&ok,2);
    }
    else
    {
        Value = leVal->text().toULongLong(&ok,10);
    }
    emit cellchanged(Num,Value);//1 num 2 value
}
/*
* @brief 设置item文本
* @note
*/
void ItemWidget::setItemText(const QString& strValue)
{
    leVal->setText(strValue);
}
/*
* @brief 获取item文本
* @note
*/
QString ItemWidget::getItemText() const
{
    return leVal->text();
}

bool ItemWidget::eventFilter(QObject* watched, QEvent* event)
{
    if ((watched == leVal) && (event->type() == QEvent::FocusIn))
    {
        static bool isfirstenter = true;
        if(2 == cbSelection->currentIndex() && isfirstenter)
        {
            isfirstenter = false;
            QDialog* dialog = new QDialog;
            dialog->setModal(false);
            BitWidget* bitwidget = new BitWidget(32);
            QGridLayout* layout = new QGridLayout;
            layout->addWidget(bitwidget);
            dialog->setLayout(layout);
            dialog->setWindowTitle("设置");
            dialog->setFixedSize(bitwidget->width() + 200,100);
            quint64 Value = leVal->text().toULongLong(NULL,2);
            bitwidget->setNum(Value);
            if(( QDialog::Rejected == dialog->exec() )|| (QDialog::Accepted == dialog->exec()))
            {
                quint64 value = bitwidget->getNum();
                leVal->setText(QString::number(value,2));
                delete dialog;
                isfirstenter = true;
            }
        }
    }
    return QWidget::eventFilter(watched,event);
}

